<?php

include("../utility/connection.php");
include("../model/mGammu.php");
session_start();

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $NoHp = trim(mysql_real_escape_string($_POST["txtNoHp"]));
    $Msg = trim(mysql_real_escape_string($_POST["txtMessage"]));
    $Sender = 'Gammu';

    $pageCode = $_GET["pageCode"];

    // insert ke table outbox
    $result = fnMyOutbox($NoHp, $Msg, $Sender);
    if ($result) {
        // ambil id-nya
        $GetId = fnGetId($NoHp, $Msg, $Sender);
        $SendId = mysql_result($GetId, 0);

        // kirim sms
        $command = '"C:\Gammu 1.32.0\bin\gammu.exe" -c "C:\Gammu 1.32.0\bin\gammurc" sendsms text ' . $NoHp . ' -text "' . $Msg . '"';
        $retMessage = exec($command);
        $_SESSION['MsgSent'] = 'Command: ' . $command . ', Result: ' . $retMessage;

        // cek return message
        $sSearchString = "OK, message reference=";
        $pos = strrpos($retMessage, $sSearchString);
        if ($pos === false) {
            // not found...
        } else if ($pos > 0) {
            // found, change sts_kirim to 1
            fnChangeStsKirim($SendId);
        }

        // kembali ke halaman sms
        if ($pageCode == '2') {
            header("location: ../ui/admin/smsForm.php?init=0");
        } else if ($pageCode == '1') {
            header("location: ../ui/reviewer/smsFormReviewer.php?init=0");
        }
    } else {
        $retMessage = 'Gagal Insert ke Table mst_sms_outbox!';
        $_SESSION['MsgSent'] = $retMessage;
        if ($pageCode == '2') {
            header("location: ../ui/admin/smsForm.php?init=0");
        } else if ($pageCode == '1') {
            header("location: ../ui/reviewer/smsFormReviewer.php?init=0");
        }
    }
}
?>
